package club.hicode.daydayup.rpc.demo.server.frame;


import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

/**
 * 服务端 ChannelInitializer
 * 每一个连接都会有一个此类的实例
 *
 * @author LiuCHunfu
 */
public class MicProviderServerInitializer extends ChannelInitializer<SocketChannel> {

    private static final String DELIMITER_FLAG = "$";

    @Override
    public void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();

        ByteBuf delimiter = Unpooled.copiedBuffer(DELIMITER_FLAG.getBytes());
        //  pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
        pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, delimiter));
        pipeline.addLast("decoder", new StringDecoder());
        pipeline.addLast("encoder", new StringEncoder());
        pipeline.addLast("handler", new MicProviderHandler());

        System.out.println("SimpleChatClient:" + ch.remoteAddress() + "连接上");
    }
}
